﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.IO;
using System.Linq;
using System.Text;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
namespace WPFExcelReport
{ 
    public partial class HomeDocumentData : BaseFunction
    {
        
        public HomeDocumentData()
            : base()
        {

        }
        public HomeDocumentData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }
        public HomeDocumentData(OracleConnection _myConnection, IntexReptServBLClient _proxy, string userID = "        ")
            : base(_myConnection, _proxy, userID)
        {
        }
        public DataSet LoadSetYarnPrice()
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                string filePath = Path.Combine(PathHelper.ApplicationPath, @"Documents\HLTYARNPRICE.XML");
                if (System.IO.File.Exists(filePath))
                {
                    setResult.ReadXml(filePath);
                    setResult.Tables[0].TableName = "YARNPRICE";
                    setReturn.Merge(setResult);
                }
                else
                {
                    setResult = LoadYarnPrice();
                    setReturn.Merge(setResult);
                    SaveYarnPrice(setReturn.Tables["YARNPRICE"]);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public void SaveYarnPrice(DataTable table)
        {
            DataSet SetReturn = new DataSet();
            try
            {
                SetReturn.Merge(table);
                string filePath = Path.Combine(PathHelper.ApplicationPath, @"Documents\HLTYARNPRICE.XML");
                if (System.IO.File.Exists(filePath))
                {
                    System.IO.File.Delete(filePath);
                }
                SetReturn.WriteXml(filePath);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public DataSet LoadSetProdCost()
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                string filePath = Path.Combine(PathHelper.ApplicationPath, @"Documents\PRODCOST.XML");
                if (System.IO.File.Exists(filePath))
                {
                    setResult.ReadXml(filePath);
                    setResult.Tables[0].TableName = "PRODCOST";
                }
                setReturn.Merge(setResult);
                filePath = Path.Combine(PathHelper.ApplicationPath, @"Documents\PROCESSING.XML");
                setResult = new DataSet();
                if (System.IO.File.Exists(filePath))
                {
                    setResult.ReadXml(filePath);
                    setResult.Tables[0].TableName = "PROCESSING";
                }
                setReturn.Merge(setResult);

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet ReSetSETUP(string company, string user)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataSet settemp = new DataSet();
            try
            {
                ArrayList tables = new ArrayList();
                string fileroot = PathHelper.ApplicationPath + @"\Documents\";
                string filePath = fileroot + "HLTSETUP.xls";
                if (System.IO.File.Exists(filePath))
                {
                    if (exAgent == null)
                    {
                        exAgent = new ExcelX.ExcelLib.ExcelAgent(filePath);
                    }
                    setResult = exAgent.LoadDataTableDef(filePath, "");
                    foreach (DataTable tbTemp in setResult.Tables)
                    {
                        string tbName = tbTemp.TableName;
                        settemp = new DataSet();
                        settemp.DataSetName = "HLTSETUP";
                        settemp.Merge(tbTemp);
                        filePath = fileroot + tbName + ".XML";
                        if (System.IO.File.Exists(filePath))
                        {
                            System.IO.File.Delete(filePath);
                        }
                        settemp.WriteXml(filePath);
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

            return setReturn;
        }
        public void SaveSetProdCost(DataTable table)
        {
            DataSet SetReturn = new DataSet();

            try
            {
                SetReturn.Merge(table);
                string filePath = Path.Combine(PathHelper.ApplicationPath, "HLTPRODCOST.XML");
                if (System.IO.File.Exists(filePath))
                {
                    System.IO.File.Delete(filePath);
                }
                SetReturn.WriteXml(filePath);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public DataSet LoadYarnPrice()
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT '' as YID,GSART as YSUPID, GSART AS YYARN,GSVAR AS YVAR,'          ' AS YCOLOR,'HLT' AS YSUPNAME,'0' AS YCPRICE,'KG' AS YUOM,To_char(SYSDATE,'YYYYMMDD') AS YEFFECTDATE,'INTEX' AS YUSER ,"
                    + "  To_char(SYSDATE,'YYYYMMDD') AS YDATE, '' AS YREMARK1,'' AS YREMARK2, '' AS YREMARK3 FROM GSDAT D"
                    + "  WHERE GSFERTKZ='Z'";
                setResult = Connect2Proxy(or1);
                if (setResult.Tables.Count > 0)
                {
                    setResult.Tables[0].TableName = "YARNPRICE";
                }
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet QueryKDSTAMM()
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT * FROM INTEX.KDSTAMM WHERE ROWNUM<10";
                ;
                setResult = Connect2Proxy(or1);
                if (setResult.Tables.Count > 0)
                {
                    setResult.Tables[0].TableName = "KDSTAMM";
                }
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet LoadWAART(string UsingID, string article = "")
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            OracleCommand or1;
            try
            {
                UsingID = UsingID.Trim();
                UsingID = UsingID.Replace("\t", "");
                UsingID = Left(UsingID + "        ", 8);
                or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT DISTINCT WAART, WAVAR"
               + " FROM INTEX.WASTAMM WHERE  rownum<200 and WAKUNDNR='00000000' AND WAART like '" + article + "%' AND warfertkz = 'E' AND WAFFERTKZ = 'E' "
                + " AND (WANEUB = '" + UsingID + "' OR '" + UsingID + "' IN ('FPDE    ','XHJUN   ')) order by 1 desc";
                setResult = Connect2Proxy(or1);
                if (setResult.Tables.Count > 0)
                {
                    setResult.Tables[0].TableName = "WAART";
                }
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet GetData(DataRow dwInput)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataRow dwART = null;
            try
            {
                //KAAART,KAAVAR,KAAFARBE,KAABESCHI
                setReturn.Merge(dwInput.Table.Copy());
           
            }
            catch (Exception ex)
            {
                Tracer.Error(ABPOS + ex.StackTrace, ex);
            }
            finally
            {
                EventDispatcher.Instance.RaiseStatus("完成已读取纱线信息", StatusEventType.StopProgress);
            }
            return setReturn;
        }
      
    }
}
